import Vue from 'vue';
import Router from 'vue-router';
import setRouter from '@/router/set';
import Layout from '@/components/common/Home'

Vue.use(Router);

const accessRoutes = JSON.parse(localStorage.getItem('routers'))
let newRouter = [];
if (accessRoutes) {
    newRouter = setRouter(accessRoutes)
}
// // 替换现有router的routes
const  router = new Router({
    // mode: 'history',
    routes: [
        {
            path: '/',
            redirect: '/home'
        },
        ...newRouter,
        {
            path: '/user',
            component: Layout,
            hidden: true,
            children: [
                {
                    path: 'profile',
                    component: () => import('../components/page/system/user/profile'),
                    name: 'Profile',
                    meta: {title: '个人中心'}
                }
            ]
        },
        {
            path: '/system/role-auth',
            component: Layout,
            hidden: true,
            children: [
                {
                    path: 'user/:roleId(\\d+)',
                    component: () => import('../components/page/system/role/authUser'),
                    name: 'AuthUser',
                    meta: { title: '分配用户', activeMenu: '/system/role' }
                }
            ]
        },
        {
            path: '/system/user-auth',
            component: Layout,
            hidden: true,
            children: [
                {
                    path: 'role/:userId(\\d+)',
                    component: () => import('../components/page/system/user/authRole'),
                    name: 'AuthRole',
                    meta: { title: '分配角色', activeMenu: '/system/user' }
                }
            ]
        },
        {
            path: '/404',
            component: () => import('@/components/page/404.vue'),
            meta: {title: '404'}
        },
        {
            path: '/login',
            component: () => import(/* webpackChunkName: "login" */ '../components/page/Login.vue'),
            meta: {title: '登录'}
        },
        {
            path: '*',
            redirect: '/404',
            hidden: true
        }
    ]
});

// 解决重复点击导航时，控制台出现报错
const VueRouterPush = Router.prototype.push;
Router.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
};

export default router;
